import { login, GetUserInfoAPI } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
const state = {
  token: getToken() || '',
  userInfo: {}
}
const mutations = {
  setlogin(state, val) {
    state.token = val
    setToken(val)
  },
  logout(state) {
    state.token = ''
    removeToken()
  },
  setuserInfo(state, val) {
    state.userInfo = val
  }
}
const actions = {
  async login(store, value) {
    const { data } = await login(value)
    // console.log(data)
    store.commit('setlogin', data.token)
    return data
  },
  async getInfo(store) {
    const { data: { userInfo }} = await GetUserInfoAPI()
    // console.log(userInfo)
    store.commit('setuserInfo', userInfo)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

